/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.extendsplat.sys.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.gwc.extendsplat.sys.entity.SysAttachfile;
import com.alibaba.druid.util.StringUtils;
import com.gwc.extendsplat.sys.dao.SysAttachfileDao;

/**
 * 附件管理Service
 * @author 黎俊涛
 * @version 2017-04-14
 */
@Service
@Transactional(readOnly = true)
public class SysAttachfileService extends CrudService<SysAttachfileDao, SysAttachfile> {

	public SysAttachfile get(String id) {
		return super.get(id);
	}
	
	public List<SysAttachfile> findList(SysAttachfile sysAttachfile) {
		return super.findList(sysAttachfile);
	}
	
	public Page<SysAttachfile> findPage(Page<SysAttachfile> page, SysAttachfile sysAttachfile) {
		
		if(sysAttachfile.getUser()!=null&&!StringUtils.isEmpty(sysAttachfile.getUser().getId())){
			System.out.println(sysAttachfile.getLable_search()+"==========================================213");
			if(sysAttachfile.getLable_search()!=null&&sysAttachfile.getLable_search().length()>0&&!sysAttachfile.getLable_search().equals("")) {
				sysAttachfile.getSqlMap().put("dsf", " and a.id in (select sys_attachfile_id from sys_attachfile_label where file_label = '"+sysAttachfile.getLable_search()+"')"+" and ((a.user='"+sysAttachfile.getUser().getId()+"' and a.attachtype='person' ) or "
						+ "(a.attachtype='group' and (a.user='"+sysAttachfile.getUser().getId()+"' or '"+sysAttachfile.getUser().getId()+"'=adst.student)) or "
								+ "a.attachtype='all')");
			}else {
				sysAttachfile.getSqlMap().put("dsf", " and ((a.user='"+sysAttachfile.getUser().getId()+"' and a.attachtype='person' ) or "
						+ "(a.attachtype='group' and (a.user='"+sysAttachfile.getUser().getId()+"' or '"+sysAttachfile.getUser().getId()+"'=adst.student)) or "
								+ "a.attachtype='all')");
			}
			
		}
		return super.findPage(page, sysAttachfile);
	}
	public Page<SysAttachfile> findMaterialPage(Page<SysAttachfile> page, SysAttachfile sysAttachfile) {
		List<Dict> list=DictUtils.getDictList("material_type");
		
		StringBuffer sb=new StringBuffer("(");
		for(int i=0;i<list.size();i++){
			sb.append("'");
			sb.append(list.get(i).getValue());
			sb.append("'");
			if(i != list.size()-1)
				sb.append(",");
		}
		sb.append(")");
		if(sysAttachfile.getUser()!=null&&!StringUtils.isEmpty(sysAttachfile.getUser().getId())){
			sysAttachfile.getSqlMap().put("dsf", " and a.user='"+sysAttachfile.getUser().getId()+"' and (a.attachtype in "+sb.toString()+") ");
		}
		return super.findPage(page, sysAttachfile);
	}
	@Transactional(readOnly = false)
	public void save(SysAttachfile sysAttachfile) {
		
		super.save(sysAttachfile);
		String [] labels = sysAttachfile.getFile_label();
		for (String string : labels) {
			dao.saveFileLabel(sysAttachfile.getId(),string);
		}
		//System.out.println(sysAttachfile.getId()+"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
	}
	
	@Transactional(readOnly = false)
	public void delete(SysAttachfile sysAttachfile) {
		super.delete(sysAttachfile);
	}
	
}